/*
 *  Copyright 2007 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.action.core.sequenceItem;

import com.blandware.atleap.common.Constants;
import com.blandware.atleap.service.core.SequenceManager;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.ControllerSupport;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * <p>Sequence item list controller. This class is used to get min and
 * max positions.
 * </p>
 * <p><a href="SequenceItemListController.java.html"><i>View Source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version : $Revision: 1.4 $ : 2007/12/17 13:38:25 $
 */
public final class SequenceItemListController extends ControllerSupport {
	//~ Methods ================================================================
	/**
	 * Retrieves owners' infos and puts them into request to use on the page
     * with list of sequence items.
	 *
	 * @param tilesContext   Current tile context
	 * @param request        Current request
	 * @param response       Current response
	 * @param servletContext Current Servlet Context
	 */
	public void execute(ComponentContext tilesContext, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) throws Exception {

		// owner sequence ID
		Long sequenceId = (Long) request.getAttribute("sequenceId");

		ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		SequenceManager sequenceManager = (SequenceManager) applicationContext.getBean(Constants.SEQUENCE_MANAGER_BEAN);

        Integer[] positions = sequenceManager.getSequenceItemFirstLastOccupiedPositions(sequenceId);
        Integer minPosition = positions[0];
        Integer maxPosition = positions[1];

        request.setAttribute("minPosition", minPosition);
        request.setAttribute("maxPosition", maxPosition);
    }
}